<?php
require 'TVB/pms/pdes/common/bootstrap.php';

if(isset($_REQUEST['premium']) && $_REQUEST['premium']){
	    $f = new p_folder();
}else{
	    $f = new folder();
}

empty($_REQUEST['id']) && exit('id is null');

$format = empty($_REQUEST['format']) ? 'json' : $_REQUEST['format'];
$ptype  = empty($_REQUEST['ptype']) ? 'programme' : $_REQUEST['ptype'];
$id     = (int)$_REQUEST['id'];

if('json' == $format)
{
    $folders = $f->get_folder_items($id);
	echo json_encode($folders);
}
else if('html' == $format)
{
    $folders = $f->get_folder_items($id);
    $folder_detail = $f->get($id);

	$str_item_list   = '';
	$str_ftype_class = $f->is_video_folder($id) ? 'video' : 'image';
	$str_ftype_id    = $folder_detail->folder_type_id;

	foreach($folders as $item){
		$item['title'] = 'ID:'.$item['id'].'<br />' 
						 . htmlentities($item['title'], ENT_QUOTES, 'UTF-8');
		$item['flag']  = episode_video::getEpisodeID($item['id']);
		$str_item_list .= '<li><div>';
		$str_item_list .= '<span></span>';// . AC::$op_status[ $item['status'] ] .'</span>';
		$str_item_list .= '<a class="preview '.$str_ftype_class.'" href="'.$item['src'].'" title="Click for Preview" target="_blank">';
		$str_item_list .= '<img src="'.$item['thumb'].'" /></a>';
		$str_item_list .= '<p title="'.str_replace('<br />'," \n ",$item['title']).'">'.mb_strimwidth($item['title'],0,33,'...','UTF-8').'</p>';
		//$str_item_list .= '<p title="'.$item['title'].'">'.mb_strimwidth($item['title'],0,25,'...','UTF-8').'</p>';
		if ($item['src'] && $str_ftype_class == 'video' && $item['flag'] !== false && $str_ftype_id == AC::VIDEO_CATCHUP) {
			$str_item_list .= '<a class="btn edit chapter-marking ui-state-default ui-corner-all" href=#'.$item['id'].'>Chapter Marking</a>';
		}
		$str_item_list .= '</div></li>';
	}
	echo $str_item_list;
}
else if ('jqgrid' == $format) {
    $page = $_REQUEST['page'] ? $_REQUEST['page'] : 1;
    $page_num = $_REQUEST['rows'] ? $_REQUEST['rows'] : 10;
    $order_field = $_REQUEST['sidx'] ? $_REQUEST['sidx'] : 'modified_at';
    $order = $_REQUEST['sord'] ? $_REQUEST['sord'] : 'ASC';
    $unpublish = isset($_REQUEST['unpublish']) ? $_REQUEST['unpublish'] : 0;
    $act = isset($_REQUEST['act']) ? $_REQUEST['act'] : 'view';
    $list = $f->get_folder_videos($id, $page, $page_num, $order_field, $order, $unpublish, $act); 
    exit(json_encode($list));
}

